<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
	/**
	 * Authenticates a user.
	 * The example implementation makes sure if the username and password
	 * are both 'demo'.
	 * In practical applications, this should be changed to authenticate
	 * against some persistent user identity storage (e.g. database).
	 * @return boolean whether authentication succeeds.
	 */
	 /*
	public function authenticate()
	{
		$users=array(
			// username => password
			'demo'=>'demo',
			'admin'=>'admin',
		);
		if(!isset($users[$this->username]))
			$this->errorCode=self::ERROR_USERNAME_INVALID;
		else if($users[$this->username]!==$this->password)
			$this->errorCode=self::ERROR_PASSWORD_INVALID;
		else
			$this->errorCode=self::ERROR_NONE;
		return !$this->errorCode;
	}
	*/
	private $_id;
	private $_jenis;
	
    public function authenticate()
    {
        $user=PenyediaTravel::model()->findByAttributes(array('USERNAME_TRAVEL'=>$this->username));
		$user2=Bus::model()->findByAttributes(array('USERNAME_BUS'=>$this->username));
		$user3=KeretaApi::model()->findByAttributes(array('USERNAME_KERETA'=>$this->username));
		
		$admin=array(
			// username => password
			'admin'=>'admingo3s',
		);
		
        if($user!==null)
        {
         	 // echo 'user 1';
			 // echo $this->id;
			// if($user->PASSWORD!==$user->encrypt($this->password))
			$_jenis = 'travel';
			if($user->PASSWORD!==($this->password))
		    { 
           		$this->errorCode=self::ERROR_PASSWORD_INVALID;
      	  	}
			else
			{
              $this->_id = $user->ID_TRAVEL;
              if(null===$user->LAST_LOGIN)
              {
                $lastLogin = time();
        	  }
		         else
              {
          		$lastLogin = strtotime($user->LAST_LOGIN);
        	  }
             $this->setState('lastLogin', $lastLogin); $this->errorCode=self::ERROR_NONE;
			}
         }
		else if($user2!==null)
        {
			$_jenis = 'bus';
			 if($user2->PASSWORD!==$this->password)
		    { 
           		$this->errorCode=self::ERROR_PASSWORD_INVALID;
      	  	}
			else
			{
              $this->_id = $user2->ID_BUS;
              if(null===$user2->LAST_LOGIN)
              {
                $lastLogin = time();
        	  }
		         else
              {
          		$lastLogin = strtotime($user2->LAST_LOGIN);
        	  }
            $this->setState('lastLogin', $lastLogin); $this->errorCode=self::ERROR_NONE;
			}
        }
		else if($user3!==null)
        {
			$_jenis = 'kereta';
			 if($user3->PASSWORD_KERETA!==$this->password)
		    { 
           		$this->errorCode=self::ERROR_PASSWORD_INVALID;
      	  	}
			else
			{
              $this->_id = $user3->ID_KERETA;
              if(null===$user3->LAST_LOGIN)
              {
                $lastLogin = time();
        	  }
		         else
              {
          		$lastLogin = strtotime($user3->LAST_LOGIN);
        	  }
            $this->setState('lastLogin', $lastLogin); $this->errorCode=self::ERROR_NONE;
			}
        }
		else if($user===null && $user2===null && $user3===null)
		{
			$this->errorCode=self::ERROR_USERNAME_INVALID;
		}
        return !$this->errorCode;
    }
	
    public function getId()
    {
        return $this->_id;
    }
	
	 public function getJenis()
    {
        return $this->_jenis;
    }

}
?>